<template>
	<view class="container">
		<view class="amount">
			<view class="balance-info">
				<text>余额：<text class="balance-value">{{userInfo.balance || 0}}</text>元</text>
			</view>
			<!-- <view class="recharge-btn" @click="goToRecharge">
				<text>充值</text>
			</view> -->
		</view>
		<view v-if="list.length > 0">
			<view class="items" v-for="(item, index) in list" :key="item.id">
				<view class="item-left">
					<text class="type">{{getTypeText(item.type)}}</text>
					<text class="time">{{item.create_time.replace(' ', '\n')}}</text>
				</view>
				<view class="item-right">
          <view style="width: 100%;display: flex;align-items: center;justify-content: space-between">
            <text>
              {{ item.product_name || '' }}
            </text>
            <text class="price" :class="{'income': item.amount > 0}">{{item.amount > 0 ? '+' : ''}}{{item.amount}}</text>
          </view>
					<text class="description" v-if="item.description">{{item.description}}</text>
				</view>
			</view>
			<!-- 加载更多 -->
			<view class="loading-more" v-if="hasMore">
				<text v-if="loading">加载中...</text>
				<text v-else>上拉加载更多</text>
			</view>
			<view class="no-more" v-else>
				<text>没有更多数据了</text>
			</view>
		</view>
		<!-- 内容为空 -->
		<view class="empty" v-else>
			<u-empty text="暂无数据" mode="data"></u-empty>
		</view>
	</view>
</template>

<script>
	import { BalanceLogTypeTextEnum } from '../../utils/enum'
	export default {
		data() {
			return {
				list: [],
				pageNo: 1,
				pageSize: 10,
				total: 0,
				loading: false,
				hasMore: true
			}
		},
		onShow() {
			this.$store.dispatch('getUserInfo')
			this.getBalanceRecords()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.pageNo = 1
			this.hasMore = true
			this.getBalanceRecords().then(() => {
				uni.stopPullDownRefresh()
			})
		},
		// 上拉加载更多
		onReachBottom() {
			if (this.hasMore && !this.loading) {
				this.pageNo++
				this.getBalanceRecords()
			}
		},
		methods: {
			async getBalanceRecords() {
				if (this.loading) return
				this.loading = true
				try {
					const res = await this.$api.getBalanceRecordPage({
						pageNo: this.pageNo,
						pageSize: this.pageSize,
						user_id: this.userInfo.id
					})
					if (this.pageNo === 1) {
						this.list = res.list || []
					} else {
						this.list = [...this.list, ...(res.list || [])]
					}
					this.total = res.total || 0
					this.hasMore = this.list.length < this.total
				} catch (error) {
					console.error('获取余额记录失败:', error)
					uni.showToast({
						title: '获取余额记录失败',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			getTypeText(type) {
				console.log('BalanceLogTypeTextEnum',BalanceLogTypeTextEnum)
				return BalanceLogTypeTextEnum[type] || '其他'
			},
			goToRecharge() {
				uni.navigateTo({
					url: '/pages/mine/recharge'
				})
			}
		}
	}
</script>

<style>
	page {
		background: #F8F9FA;
	}
</style>

<style lang="scss" scoped>
	.container {
		margin: 20rpx 30rpx;
	}

	.amount {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 0rpx 0rpx 40rpx 0rpx;
		font-size: 30rpx;

		.balance-info {
			text {
				font-size: 30rpx;
				
				.balance-value {
					font-size: 96rpx;
					font-weight: bold;
				}
			}
		}

		.recharge-btn {
			background: #FFC300;
			color: #FFFFFF;
			border: none;
			border-radius: 24rpx;
			padding: 16rpx 32rpx;
			font-size: 28rpx;
			font-weight: bold;
			cursor: pointer;

			&:active {
				opacity: 0.8;
			}

			text {
				color: #FFFFFF;
			}
		}
	}

	.items {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 32rpx 20rpx;
		margin-bottom: 22rpx;

		.item-left {
			display: flex;
			flex-direction: column;
      flex-shrink: 0;
      margin-right: 16rpx;

			.type {
				font-size: 30rpx;
				font-weight: 600;
			}

			.time {
				margin-top: 10rpx;
				font-size: 20rpx;
				color: #999;
			}
		}

		.item-right {
			display: flex;
			flex-direction: column;
			align-items: flex-end;

			.price {
				font-size: 32rpx;
				font-weight: 600;
				color: #F55726;

				&.income {
					color: #07c160;
				}
			}

			.description {
				font-size: 24rpx;
				color: #999;
				margin-top: 8rpx;
			}
		}
	}

	.loading-more, .no-more {
		text-align: center;
		padding: 20rpx 0;
		color: #999;
		font-size: 24rpx;
	}

	.empty {
		margin-top: 200rpx;
	}
</style>
